﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml;
using System.IO;

namespace UploadBlobToAzure
{
    public partial class EditStorageInfo : Form
    {
        string oldAcctValue;
        field_validator fv = new field_validator();
        public EditStorageInfo()
        {
            InitializeComponent();
            if (File.Exists("uploadblobtoazure.xml"))
            {
                try
                {
                    XmlDocument doc = new XmlDocument();
                    doc.Load("uploadblobtoazure.xml");
                    XmlNodeList xmlnl = doc.SelectNodes("xml/configuration");
                    if (!cmboStorageAccount.Items.Contains("---Select A Storage Account To Edit---"))
                    {
                        cmboStorageAccount.Items.Add("---Select A Storage Account To Edit---");
                    }
                    cmboStorageAccount.Text = cmboStorageAccount.Items[0].ToString();
                    foreach (XmlNode xml in xmlnl)
                    {
                        XmlNode node = xml.SelectSingleNode("storageaccount");
                        cmboStorageAccount.Items.Add(node.InnerText);
                    }
                    cmboStorageAccount.DropDownStyle = ComboBoxStyle.DropDownList;
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                    Application.Exit();
                }
            }
        }


        private void btnCommit_Click(object sender, EventArgs e)
        {
            if (fv.testIfFieldEmpty(cmboStorageAccount.Text) || (cmboStorageAccount.Text == "---Select A Storage Account To Edit---"))
            {
                MessageBox.Show("Please select a Storage Account From the Drop Down!!");
                this.DialogResult = DialogResult.None;
            }
            else if (fv.testIfFieldEmpty(txtStorageKey.Text))
            {
                MessageBox.Show("The new key value cannot be null, please enter a new value for the key or click on the discard changes button to discard the changes!!");
                this.DialogResult = DialogResult.None;
            }
            else
            {
                try
                {
                    XmlDocument doc = new XmlDocument();
                    doc.Load("uploadblobtoazure.xml");
                    XmlNodeList xmlnl = doc.SelectNodes("xml/configuration");

                    foreach (XmlNode xml in xmlnl)
                    {
                        XmlNode node = xml.SelectSingleNode("storageaccount");

                        if (node.InnerText == oldAcctValue)
                        {
                            xml.ParentNode.RemoveChild(xml);
                            doc.Save("uploadblobtoazure.xml");
                            break;
                        }
                    }

                    XmlDocument xd = new XmlDocument();
                    xd.Load("uploadblobtoazure.xml");
                    XmlNode nl = xd.SelectSingleNode("//xml");
                    XmlDocument xd2 = new XmlDocument();
                    xd2.LoadXml("<configuration>\n" + "\t<storageaccount>" + cmboStorageAccount.Text.Trim() + "</storageaccount>\n" + "\t<managementkeys>" + txtStorageKey.Text.Trim() + "</managementkeys>\n" + "</configuration>\n");
                    XmlNode n = xd.ImportNode(xd2.FirstChild, true);
                    nl.AppendChild(n);
                    xd.Save("uploadblobtoazure.xml");
                    this.Close();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                    Application.Exit();
                }
            }
        }


        private void btnDiscard_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void cmboStorageAccount_SelectedIndexChanged(object sender, EventArgs e)
        {
            oldAcctValue = cmboStorageAccount.SelectedItem.ToString();
            cmboStorageAccount.DropDownStyle = ComboBoxStyle.DropDownList;
        }

        private void cmboStorageAccount_Click(object sender, EventArgs e)
        {
            if (cmboStorageAccount.Items.Contains("---Select A Storage Account To Edit---"))
            {
                cmboStorageAccount.Items.Remove("---Select A Storage Account To Edit---");
            }
            cmboStorageAccount.DropDownStyle = ComboBoxStyle.DropDownList;
        }

        
    }
}
